package com.shunhe.zs_pda.base;

import android.content.Context;

import com.ruitu.arad.Arad;
import com.ruitu.arad.AradApplication;
import com.ruitu.arad.AradApplicationConfig;
import com.ruitu.arad.api.okhttp.PrefUtil;
import com.ruitu.arad.api.okhttp.Url;
import com.ruitu.router_module.Logg;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MyApp extends AradApplication {

    @Override
    protected AradApplicationConfig appConfig() {
        return new AradApplicationConfig();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Gl gl = new Gl();

        closeAndroid10Dialog();// 安卓
//        getOSSTokenData(getApplicationContext());

        if (null != Arad.preferences) {
            Arad.preferences.putString(PrefUtil.OSS_BUCKET, "fyyf-qhl").flush();
            Arad.preferences.putString(PrefUtil.OSS_DOMAIN, "fyyf-qhl.oss-cn-hangzhou.aliyuncs.com").flush();
            Arad.preferences.putString(PrefUtil.OSS_ENDPOINT, "oss-cn-hangzhou.aliyuncs.com").flush();
            Arad.preferences.putString(PrefUtil.OSS_SECRETID, "LTAI1xi0ozQB2SNK").flush();
            Arad.preferences.putString(PrefUtil.OSS_SECRETKEY, "aiQTe6fGizVgcoJSImhlOfym9eyysd").flush();

        }

//        JPushInterface.setDebugMode(true);
//        JPushInterface.init(this);
    }

    public void closeAndroid10Dialog() {
        try {
            Class aClass = Class.forName("android.content.pm.PackageParser$Package");
            Constructor declaredConstructor = aClass.getDeclaredConstructor(String.class);
            declaredConstructor.setAccessible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            Class cls = Class.forName("android.app.ActivityThread");
            Method declaredMethod = cls.getDeclaredMethod("currentActivityThread");
            declaredMethod.setAccessible(true);
            Object activityThread = declaredMethod.invoke(null);
            Field mHiddenApiWarningShown = cls.getDeclaredField("mHiddenApiWarningShown");
            mHiddenApiWarningShown.setAccessible(true);
            mHiddenApiWarningShown.setBoolean(activityThread, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void getOSSTokenData(final Context context) {
        // 发送请求
        String url = Url.GET_OSS_TOKEN;
        OkHttpClient okHttpClient = new OkHttpClient();
        try {
            Request request = new Request.Builder().url(url).tag(1).get().build();
            Call call = okHttpClient.newCall(request);
            call.enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    e.printStackTrace();
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    try {
                        String jsonString = response.body().string();
                        Logg.i("jsonString = " + jsonString);

                        JSONObject jsonObject = new JSONObject(jsonString);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
